
public class SchavaNumber extends Atom {
	public SchavaNumber(String _string) {
		super(_string);
		try {
			setValue(new Integer(Integer.parseInt(_string)));
			return;
		} catch (NumberFormatException e) {

		}
		try {
			setValue(new Double(Double.parseDouble(_string)));
			return;
		} catch (NumberFormatException e) {

		}
		throw new SchavaException("Cannot parse number " + _string);
	}

	public SchavaNumber(int num) {
		super(num);
		setValue(new Integer(num));
	}

	public SchavaNumber(double num) {
		super(num);
		setValue(new Double(num));
	}
	
	public boolean isTrue() {
		return true;
	}
	

	public boolean isNumber() {
		return true;
	}

	public SchavaNumber add(SchavaNumber other) {
		try {
			int myNum = ((Integer)getValue()).intValue();
			int otherNum = ((Integer)other.getValue()).intValue();
			return new SchavaNumber(myNum + otherNum);
		} catch (ClassCastException e) {
			
		}
		try {
			double myNum = ((Double)getValue()).doubleValue();
			double otherNum = ((Double)other.getValue()).doubleValue();
			return new SchavaNumber(myNum + otherNum);
		} catch (ClassCastException e) {
			
		}
		try {
			int myNum = ((Integer)getValue()).intValue();
			double otherNum = ((Double)other.getValue()).doubleValue();
			double value = (((double)myNum) + (otherNum));
			return new SchavaNumber(value);
		} catch (ClassCastException e) {
			
		}
		try {
			double myNum = ((Double)getValue()).doubleValue();
			int otherNum = ((Integer)other.getValue()).intValue();
			double value = ((myNum) + ((double)otherNum));
			return new SchavaNumber(value);
		} catch (ClassCastException e) {
			
		}
		throw new SchavaException("SchavaNumber: cannot add " + other);
	}

	public SchavaNumber mul(SchavaNumber other) {
		try {
			int myNum = ((Integer)getValue()).intValue();
			int otherNum = ((Integer)other.getValue()).intValue();
			return new SchavaNumber(myNum * otherNum);
		} catch (ClassCastException e) {
			
		}
		try {
			double myNum = ((Double)getValue()).doubleValue();
			double otherNum = ((Double)other.getValue()).doubleValue();
			return new SchavaNumber(myNum * otherNum);
		} catch (ClassCastException e) {
			
		}
		try {
			int myNum = ((Integer)getValue()).intValue();
			double otherNum = ((Double)other.getValue()).doubleValue();
			double value = (((double)myNum) * (otherNum));
			return new SchavaNumber(value);
		} catch (ClassCastException e) {
			
		}
		try {
			double myNum = ((Double)getValue()).doubleValue();
			int otherNum = ((Integer)other.getValue()).intValue();
			double value = ((myNum) * ((double)otherNum));
			return new SchavaNumber(value);
		} catch (ClassCastException e) {
			
		}
		throw new SchavaException("SchavaNumber: cannot multiply " + other);
	}

	public SchavaNumber sub(SchavaNumber other) {
		try {
			int myNum = ((Integer)getValue()).intValue();
			int otherNum = ((Integer)other.getValue()).intValue();
			return new SchavaNumber(myNum - otherNum);
		} catch (ClassCastException e) {
			
		}
		try {
			double myNum = ((Double)getValue()).doubleValue();
			double otherNum = ((Double)other.getValue()).doubleValue();
			return new SchavaNumber(myNum - otherNum);
		} catch (ClassCastException e) {
			
		}
		try {
			int myNum = ((Integer)getValue()).intValue();
			double otherNum = ((Double)other.getValue()).doubleValue();
			double value = (((double)myNum) - (otherNum));
			return new SchavaNumber(value);
		} catch (ClassCastException e) {
			
		}
		try {
			double myNum = ((Double)getValue()).doubleValue();
			int otherNum = ((Integer)other.getValue()).intValue();
			double value = ((myNum) - ((double)otherNum));
			return new SchavaNumber(value);
		} catch (ClassCastException e) {
			
		}
		throw new SchavaException("SchavaNumber: cannot subtract " + other);
	}

	public SchavaNumber div(SchavaNumber other) {
		try {
			int myNum = ((Integer)getValue()).intValue();
			int otherNum = ((Integer)other.getValue()).intValue();
			if ((myNum % otherNum) != 0) {
				return new SchavaNumber ((double)myNum / (double)otherNum);
			}
			return new SchavaNumber(myNum / otherNum);
		} catch (ClassCastException e) {
			
		}
		try {
			double myNum = ((Double)getValue()).doubleValue();
			double otherNum = ((Double)other.getValue()).doubleValue();
			return new SchavaNumber(myNum / otherNum);
		} catch (ClassCastException e) {
			
		}
		try {
			int myNum = ((Integer)getValue()).intValue();
			double otherNum = ((Double)other.getValue()).doubleValue();
			double value = (((double)myNum) / (otherNum));
			return new SchavaNumber(value);
		} catch (ClassCastException e) {
			
		}
		try {
			double myNum = ((Double)getValue()).doubleValue();
			int otherNum = ((Integer)other.getValue()).intValue();
			double value = ((myNum) / ((double)otherNum));
			return new SchavaNumber(value);
		} catch (ClassCastException e) {
			
		}
		throw new SchavaException("SchavaNumber: cannot divide " + other);
	}

	public int compareTo(Data o) {
		if (this.getValue() instanceof Integer) {
			return ((Integer)this.getValue()).intValue() - ((Integer)o.getValue()).intValue();
		}
		if (this.getValue() instanceof Double) {
			return ((Double)this.getValue()).compareTo((Double)o.getValue());
		}
		
		return (Integer.parseInt(this.toString()) 
				- Integer.parseInt(o.toString()));
//		double myNum = ((Double)getValue()).doubleValue();
//		double oNum = ((Double)o.getValue()).doubleValue();
//
//		int dif = (int) myNum - (int) oNum;
//		if (dif == 0) {
//			if (dif > 0) {
//				return 1;
//			} else if (dif < 0) {
//				return -1;
//			}
//			return 0;
//		}
//		return dif;
	}
}
